package com.dyzx.hbb.modules.mtn.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.mtn.entity.MtnCarVehicle;
import com.dyzx.hbb.modules.mtn.query.MtnCarVehicleQuery;
import com.dyzx.hbb.modules.mtn.service.MtnCarVehicleService;
import com.dyzx.hbb.modules.mtn.vo.MtnCarVehicleUVO;
import com.dyzx.hbb.modules.mtn.vo.MtnCarVehicleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 车辆信息控制器
 * <p>处理维修模块中的车辆信息管理，包括车辆基本信息的CRUD操作和特殊查询
 * <p>主要职责：
 * <ul>
 * <li>提供车辆信息的基础CRUD接口</li>
 * <li>实现按VIN码查询车辆详细信息</li>
 * <li>支持车辆与品牌、车型等关联数据的查询</li>
 * <li>处理车辆信息的验证逻辑</li>
 * </ul>
 * <p>设计意图：为维修管理模块提供车辆信息的统一管理功能，
 * 作为维修订单和服务的基础数据支持
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "01.车辆管理", description = "车辆管理相关接口，包括车辆信息的创建、查询、更新和删除等操作")
@RestController
@RequestMapping("/mtn/car/vehicle")
public class MtnCarVehicleController extends BaseValidController<MtnCarVehicleService, MtnCarVehicle, MtnCarVehicleQuery, MtnCarVehicleVO, MtnCarVehicleUVO> {

    @Autowired
    private MtnCarVehicleService service;

    /**
     * 添加车辆信息时的验证
     * @param entity 车辆信息VO
     * @return 验证通过的车辆实体
     */
    @Override
    protected MtnCarVehicle addValid(MtnCarVehicleVO entity) {
        return null;
    }

    /**
     * 编辑车辆信息时的验证
     * @param entity 车辆信息更新VO
     * @return 验证通过的车辆实体
     */
    @Override
    protected MtnCarVehicle editValid(MtnCarVehicleUVO entity) {
        return null;
    }

    /**
     * 获取基础服务类
     * @return 车辆服务类实例
     */
    @Override
    protected MtnCarVehicleService getBaseService() {
        return service;
    }

    /**
     * 构建查询条件包装器
     * @param query 查询参数
     * @return 查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<MtnCarVehicle> queryWrapper(MtnCarVehicleQuery query) {
        return null;
    }

    /**
     * 构建详情查询条件包装器
     * @return 详情查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<MtnCarVehicle> queryInfoWrapper() {
        MyLambdaQueryWrapper<MtnCarVehicle> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(MtnCarVehicle::getId, MtnCarVehicle::getVin, MtnCarVehicle::getEngineNo, MtnCarVehicle::getBrand, MtnCarVehicle::getModel);
        queryWrapper.select(MtnCarVehicle::getColor, MtnCarVehicle::getCarModalId);
        queryWrapper.select("(SELECT model FROM car_model WHERE id = mtn_car_vehicle.car_modal_id LIMIT 1)").as("car_modal_name");
        queryWrapper.select("(SELECT logo FROM car_model WHERE id = mtn_car_vehicle.car_modal_id LIMIT 1)").as("car_modal_logo");
        queryWrapper.select("(SELECT name FROM car_mark WHERE id = mtn_car_vehicle.car_mark_id LIMIT 1)").as("mark_name");
        queryWrapper.select("(SELECT logo FROM car_mark WHERE id = mtn_car_vehicle.car_mark_id  LIMIT 1)").as("mark_logo");
        queryWrapper.select("(SELECT trans_mission FROM car_model_area WHERE id = mtn_car_vehicle.car_modal_area_id  LIMIT 1)").as("trans_mission");
        queryWrapper.select(MtnCarVehicle::getStatus, MtnCarVehicle::getCreateDate, MtnCarVehicle::getUpdateDate);
        queryWrapper.userName(MtnCarVehicle::getOperatorId).as("operator_name");
        return queryWrapper;
    }

    /**
     * 根据VIN码查询车辆信息
     * @param vin 车辆识别码
     * @return 车辆信息详情
     */
    @Operation(summary = "根据VIN码查询车辆信息", description = "通过车辆唯一识别码(VIN)查询车辆的详细信息")
    @GetMapping("/vin/{vin}")
    public Result<Map<String, Object>> getByVin(@Parameter(description = "车辆识别码") @PathVariable String vin) {
        MyLambdaQueryWrapper<MtnCarVehicle> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(MtnCarVehicle::getVin, vin);
        queryWrapper.select(MtnCarVehicle::getId, MtnCarVehicle::getVin, MtnCarVehicle::getEngineNo, MtnCarVehicle::getBrand, MtnCarVehicle::getModel);
        queryWrapper.select(MtnCarVehicle::getColor, MtnCarVehicle::getCarModalId);
        queryWrapper.select("(SELECT model FROM car_model WHERE id = mtn_car_vehicle.car_modal_id LIMIT 1)").as("car_modal_name");
        queryWrapper.select("(SELECT logo FROM car_model WHERE id = mtn_car_vehicle.car_modal_id LIMIT 1)").as("car_modal_logo");
        queryWrapper.select("(SELECT name FROM car_mark WHERE id = mtn_car_vehicle.car_mark_id LIMIT 1)").as("mark_name");
        queryWrapper.select("(SELECT logo FROM car_mark WHERE id = mtn_car_vehicle.car_mark_id  LIMIT 1)").as("mark_logo");
        queryWrapper.select("(SELECT trans_mission FROM car_model_area WHERE id = mtn_car_vehicle.car_modal_area_id  LIMIT 1)").as("trans_mission");
        queryWrapper.select(MtnCarVehicle::getStatus, MtnCarVehicle::getCreateDate, MtnCarVehicle::getUpdateDate);
        queryWrapper.userName(MtnCarVehicle::getOperatorId).as("operator_name");

        Map<String, Object> map = service.getMap(queryWrapper);
        return map == null ? Result.error(502,"未查询到车辆信息") :Result.ok(map);

    }
}
